package com.buddies.lottery.data.repo;

import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.data.HandleResultHelper;
import com.buddies.lottery.data.RetrofitManager;
import com.buddies.lottery.data.api.UserApi;
import com.buddies.lottery.data.bean.BankResponse;
import com.buddies.lottery.data.bean.PayRecord;
import com.buddies.lottery.data.bean.PayType;
import com.buddies.lottery.data.bean.User;
import com.buddies.lottery.data.bean.Wallet;
import com.buddies.lottery.data.bean.WithdrawRecord;
import com.buddies.lottery.data.resp.BalanceResponse;
import com.buddies.lottery.data.resp.CheckWalletPwdResponse;
import com.buddies.lottery.data.resp.LoginResponse;
import com.buddies.lottery.data.resp.PayResponse;
import com.buddies.lottery.data.resp.PayResultResponse;
import com.buddies.lottery.data.resp.WithdrawResponse;
import com.buddies.lottery.data.result.PageResult;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * Description:
 * Date: 2018/2/24
 * Author: xuliang
 */
public class UserRepo {

    private UserApi mUserApi;

    public UserRepo() {
        mUserApi = RetrofitManager.getInstance().create(UserApi.class);
    }

    /* 聊天的时候显示头像和昵称 */
    public Observable<User> getUserInfo(long userId) {
        return mUserApi.getUserInfo(userId).compose(HandleResultHelper.<User>handleResult())
                .flatMap(new Function<User, ObservableSource<User>>() {
                    @Override
                    public ObservableSource<User> apply(User user) throws Exception {
                        ConfigManager.getInstance().setUser(user);
                        return Observable.just(user);
                    }
                });
    }

    public Observable<LoginResponse> postLogin(String userId, String password) {
        return mUserApi.postLogin(userId, password).compose(HandleResultHelper.<LoginResponse>handleResult());
    }

    public Observable<LoginResponse> postRegister(String userId, String password) {
        return mUserApi.postRegister(userId, password).compose(HandleResultHelper.<LoginResponse>handleResult());
    }

    public Observable<LoginResponse> postResetPassword(String oldPassword, String newPassword) {
        return mUserApi.postUpdatePassword(oldPassword, newPassword).compose(HandleResultHelper.<LoginResponse>handleResult());
    }

    public Observable<Object> postUpdateWalletPwd(String oldPassword, String newPassword) {
        return mUserApi.postUpdateWalletPwd(oldPassword, newPassword).compose(HandleResultHelper.handleResult());
    }

    public Observable<Object> postUpdateBank(String cardName, String cardNo, String cardUser, String walletPwd) {
        return mUserApi.postUpdateBank(cardName, cardNo, cardUser, walletPwd).compose(HandleResultHelper.handleResult());
    }

    public Observable<PayResultResponse> postPay(String payType, String money) {
        return mUserApi.postPay(payType, money).compose(HandleResultHelper.<PayResultResponse>handleResult());
    }

    public Observable<BalanceResponse> getBalance() {
        return mUserApi.getBalance().compose(HandleResultHelper.<BalanceResponse>handleResult());
    }

    public Observable<PayResponse> getPayQrcode() {
        return mUserApi.getPayQrcode().compose(HandleResultHelper.<PayResponse>handleResult());
    }

    public Observable<PageResult<PayRecord>> getPayRecord(Map<String, Object> params, int page) {
        return mUserApi.getPayRecord((String) params.get("payType"), page).compose(HandleResultHelper.<PageResult<PayRecord>>handleResult());
    }

    public Observable<Wallet> getWallet() {
        return mUserApi.getWallet().compose(HandleResultHelper.<Wallet>handleResult());
    }

    public Observable<CheckWalletPwdResponse> checkWalletPwd(String pwd) {
        return mUserApi.checkWalletPwd(pwd).compose(HandleResultHelper.<CheckWalletPwdResponse>handleResult());
    }

    public Observable<BankResponse> getBank() {
        return mUserApi.getBank().compose(HandleResultHelper.<BankResponse>handleResult());
    }

    public Observable<Object> postQrcode(String qrcodeUrl) {
        return mUserApi.postQrcode(qrcodeUrl).compose(HandleResultHelper.<Object>handleResult());
    }

    public Observable<WithdrawResponse> getWithdrawInfo() {
        return mUserApi.getWithdrawInfo().compose(HandleResultHelper.<WithdrawResponse>handleResult());
    }

    public Observable<Wallet> postWithdraw(int money, String pwd) {
        return mUserApi.postWithdraw(money, pwd).compose(HandleResultHelper.<Wallet>handleResult());
    }

    public Observable<PageResult<PayType>> getPayTypeList() {
        return mUserApi.getPayTypeList().compose(HandleResultHelper.<PageResult<PayType>>handleResult());
    }

    public Observable<PageResult<WithdrawRecord>> getWithdrawRecord(int page) {
        return mUserApi.getWithdrawRecord(page).compose(HandleResultHelper.<PageResult<WithdrawRecord>>handleResult());
    }

    public Observable<LoginResponse> postAgentRegister(int userType, String userId, String password, float rebate) {
        return mUserApi.postAgentRegister(userType, userId, password, rebate).compose(HandleResultHelper.<LoginResponse>handleResult());
    }

}
